var t = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), a = require("../../@babel/runtime/helpers/defineProperty"), s = getApp();

Page({
    data: {
        activity_id: 0,
        activity_data: {},
        hand_goods_obj: {},
        hand_goods_id: 0,
        shareShow: !1,
        specsShow: !1,
        goodsPopupShow: !1,
        cartListShow: !1,
        currentTime: parseInt(new Date().getTime() / 1e3),
        timeData: {},
        shareOptions: [ {
            name: "生成海报",
            icon: "poster"
        }, {
            name: "分享给好友",
            icon: "wechat",
            openType: "share"
        } ],
        goods_list: [],
        buyerListData: [],
        carList: [],
        goodsCartsTotal: {},
        language: {},
        posterShow: !1,
        cartsNum: 0,
        cartsTotal: 0,
        cart_ids: [],
        good_ids: [],
        showChoose: !1,
        radioChoose: "",
        dispatch_types: [],
        networkLoading: !1,
        buyerListDataLimit: [],
        page: 1,
        isLoadMore: !0,
        total_page: 0,
        end_time: ""
    },
    onChangeTime: function(t) {
        this.setData({
            timeData: t.detail
        });
    },
    onCloseShare: function() {
        this.setData({
            shareShow: !1
        });
    },
    close_yz_cartList_popup: function() {
        this.setData({
            cartListShow: !1
        });
    },
    typeChange: function(t) {
        this.setData({
            radioChoose: Number(t.detail)
        });
    },
    close_yz_specs_popup: function(t) {
        console.log(t), 1 == t.detail.clicktype ? (this.addCartRequest({
            goods: {
                id: t.detail.goodsId
            },
            cartid: t.detail.cartid,
            total: t.detail.goodsCount,
            optionsId: t.detail.optionsId
        }), this.setData({
            specsShow: !1
        })) : this.setData({
            specsShow: !1
        });
    },
    onLoad: function(t) {
        var a = t.activity_id, e = decodeURIComponent(t.scene);
        if (e) {
            var o = [];
            o = e.split("&"), console.log(o);
            for (var i = 0; i < o.length; i++) {
                var d;
                d = o[i].split("="), console.log(d), "activity_id" == d[0] && (a = d[1]), "mid" == d[0] && s._setMid(d[1]);
            }
            console.log(o);
        }
        t.mid && s._setMid(t.mid), a && 0 != a ? (this.setData({
            activity_id: a
        }), this.getData(a), this.getBuyerData(a), this.getCartList()) : this.tips("参数错误");
    },
    initData: function() {
        this.data.page = 1, this.data.isLoadMore = !0, this.data.total_page = 0;
    },
    getData: function(t) {
        var a = this;
        this.initData();
        var e = s.getNetAddresss("plugin.community-relay.frontend.activity.activity-detail");
        s._postNetWork({
            url: e,
            data: {
                activity_id: t
            },
            success: function(t) {
                var s = t.data;
                if (1 !== s.result) return a.tips(s.msg);
                a.data.end_time = 1e3 * (s.data.end_time - a.data.currentTime), a.setData({
                    goods_list: s.data.goods_list,
                    networkLoading: !0,
                    activity_data: s.data
                });
            }
        });
    },
    getBuyerData: function(t) {
        var a = this, e = s.getNetAddresss("plugin.community-relay.frontend.activity.activity-buyer");
        s._postNetWork({
            url: e,
            data: {
                activity_id: t
            },
            success: function(t) {
                var s = t.data;
                if (1 !== s.result) return a.tips(s.msg);
                console.log(s), a.setData({
                    buyerListDataLimit: s.data.data,
                    buyerListData: s.data.data
                }), a.data.isLoadMore = !0, a.data.total_page = s.data.last_page, a.data.total_page || (a.data.total_page = 0);
            }
        });
    },
    _getMoreData: function() {
        var t = this;
        if (this.data.isLoadMore = !1, !(this.data.page >= this.data.total_page)) {
            this.data.page += 1;
            var a = s.getNetAddresss("plugin.community-relay.frontend.activity.activity-buyer");
            s._postNetWork({
                url: a,
                data: {
                    page: this.data.page,
                    activity_id: this.options.activity_id
                },
                success: function(a) {
                    var s = a.data;
                    if (t.data.isLoadMore = !0, 1 === s.result) {
                        var e = s.data.data, o = t.data.buyerListData.concat(e);
                        t.setData({
                            buyerListData: o
                        });
                    } else t.data.page = t.data.page - 1, t.data.isLoadMore = !1;
                }
            });
        }
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    gotoOrder: function() {
        wx.navigateTo({
            url: "/packageA/member/myOrder_v2/myOrder_v2?status=0"
        });
    },
    onShow: function() {},
    onHide: function() {},
    openGoodsDetail: function(t) {
        var a = t.currentTarget.dataset.item;
        a.id == this.data.hand_goods_id ? this.setData({
            goodsPopupShow: !0
        }) : (this.data.hand_goods_id = a.id, this.getGoodsDetails(a.id, "goodsDetails"));
    },
    calculateTotal: function(t) {
        var a = {};
        for (var s in t) {
            var e = t[s].goods_id;
            this.data.good_ids.push(e), this.data.cart_ids.push(t[s].id), a[e] ? a[e].total = a[e].total + t[s].total : (a[e] = {}, 
            a[e].goods_id = t[s].goods_id, a[e].total = t[s].total, a[e].id = t[s].id);
        }
        this.setData({
            goodsCartsTotal: a
        });
    },
    getFirstCart: function(t) {
        this.data.carts = t;
        var a = 0, s = 0;
        this.data.carts.forEach(function(t) {
            a += t.total, s += t.total * t.goods.price;
        }), s = parseFloat(s).toFixed(2), this.setData({
            cartsNum: a,
            cartsTotal: s
        }), this.calculateTotal(this.data.carts);
    },
    openShareShow: function() {
        this.setData({
            shareShow: !0
        });
    },
    openCartListPopup: function() {
        this.setData({
            cartListShow: !this.data.cartListShow
        });
    },
    onSelectShare: function(t) {
        this.onCloseShare(), 0 == t.detail.index && this.setData({
            posterShow: !0
        }), console.log(t.detail.index);
    },
    openSpecs: function(t) {
        var a = t.currentTarget.dataset.item;
        a.id == this.data.hand_goods_id ? this.setData({
            specsShow: !0
        }) : (this.data.hand_goods_id = a.id, this.getGoodsDetails(a.id, "specs"));
    },
    getGoodsDetails: function(t, e) {
        var o = this, i = s.getNetAddresss("goods.goods.get-goods-page");
        s._postNetWork({
            url: i,
            data: {
                id: Number(t)
            },
            success: function(t) {
                var s, i = t.data;
                if (1 !== i.result) return o.tips(i.msg);
                var d = "";
                "goodsDetails" == e ? d = "goodsPopupShow" : "specs" == e && (d = "specsShow"), 
                i.data.get_goods.end_time = o.data.end_time, console.log("1111111111111111", o.data.end_time), 
                o.setData((a(s = {}, d, !0), a(s, "hand_goods_obj", i.data.get_goods), s));
            }
        });
    },
    onUnload: function() {},
    getCartList: function() {
        var t = this, a = this.data.activity_id, e = s.getNetAddresss("plugin.community-relay.frontend.activity.activity-cart");
        s._postNetWork({
            url: e,
            data: {
                activity_id: a
            },
            success: function(a) {
                var s = a.data;
                if (1 !== s.result) return t.tips(s.msg);
                var e = s.data;
                t.setData({
                    carList: e
                }), t.getFirstCart(e), console.log(e);
            }
        });
    },
    updateCartNum: function(t) {
        console.log(t);
        var a = t.target.dataset.item, s = t.target.dataset.cartid, e = t.detail.value, o = e;
        "" != e && 0 != e || (o = 0), this.data.goodsCartsTotal[a.id].total != o && this.reduceCartRequest({
            goods: a,
            cartid: s,
            total: o
        });
    },
    reduceCartNum: function(t) {
        var a = t.currentTarget.dataset.item, s = t.currentTarget.dataset.cartid;
        this.reduceCartRequest({
            goods: a,
            cartid: s
        });
    },
    reduceCartRequest: function(t) {
        var a = this, e = t.goods, o = t.cartid, i = t.total, d = e.id, r = 0;
        if (i || 0 === i) r = i; else {
            var n = this.data.goodsCartsTotal;
            r = Number(n[d].total) - 1, this.data.goodsCartsTotal[d].total = r;
        }
        var c = "", l = {};
        r <= 0 ? (c = s.getNetAddresss("member.member-cart.destroy"), l = {
            ids: o
        }) : (c = s.getNetAddresss("member.member-cart.updateNumV2"), l = {
            id: o,
            num: r
        }), s._getNetWork({
            url: c,
            data: l,
            success: function(t) {
                var s = t.data;
                a.tips(s.msg), 1 == s.result && a.getCartList();
            }
        });
    },
    addCartNum: function(t) {
        var a = t.currentTarget.dataset.item, s = t.currentTarget.dataset.cartid;
        console.log(a), this.addCartRequest({
            goods: a,
            cartid: s
        });
    },
    addCartRequest: function(t) {
        var a = this, e = t.goods, o = t.cartid, i = t.total, d = t.optionsId;
        if (console.log("加入购物车"), e.vip_level_status && 1 == e.vip_level_status.status) return this.tips(e.vip_level_status.tips);
        if (0 == e.stock) return this.tips("库存为零");
        var r = 1, n = "", c = {};
        if (s._isTextEmpty(this.data.goodsCartsTotal[e.id]) || 0 == o) n = s.getNetAddresss("member.member-cart.store"), 
        i && i > 0 && (r = i), c = {
            goods_id: e.id,
            total: r
        }, d && d > 0 && (c.option_id = d); else {
            if (this.data.goodsCartsTotal[e.id].total >= e.stock) return this.tips("库存不足");
            var l = this.data.goodsCartsTotal;
            r = Number(l[e.id].total) + 1, i && i > 0 && (r = i), this.data.goodsCartsTotal[e.id].total = r, 
            n = s.getNetAddresss("member.member-cart.updateNumV2"), c = {
                id: o,
                num: r
            };
        }
        s._getNetWork({
            url: n,
            data: c,
            success: function(t) {
                var s = t.data;
                a.tips(s.msg), 1 == s.result && a.getCartList();
            }
        });
    },
    cartListAdd: function(t) {
        console.log(t.detail), console.log(this.data.goodsCartsTotal), this.addCartRequest({
            goods: t.detail.goods,
            cartid: t.detail.id,
            total: t.detail.total + 1
        });
    },
    cartListReduce: function(t) {
        console.log(t.detail), this.reduceCartRequest({
            goods: t.detail.goods,
            cartid: t.detail.id,
            total: t.detail.total - 1
        });
    },
    cartListInputBlur: function(a) {
        var s, e = this.data.carList, o = t(e);
        try {
            for (o.s(); !(s = o.n()).done; ) {
                var i = s.value;
                if (console.log(a), i.id == a.detail.id && i.total == a.detail.total) return;
            }
        } catch (t) {
            o.e(t);
        } finally {
            o.f();
        }
        this.reduceCartRequest({
            goods: a.detail.goods,
            cartid: a.detail.id,
            total: a.detail.total
        });
    },
    goodsDetailCartAdd: function(t) {
        this.setData({
            goodsPopupShow: !1,
            specsShow: !0
        });
    },
    onPullDownRefresh: function() {},
    closeChoose: function() {
        this.setData({
            showChoose: !1
        });
    },
    checkGoods: function() {
        var t = this, a = s.getNetAddresss("memberCart.checkout");
        s._getNetWork({
            url: a,
            data: {
                cart_ids: this.data.cart_ids.join(",")
            },
            success: function(a) {
                var s = a.data;
                if (1 !== s.result) return t.tips(s.msg);
                1 == s.data.need_choose ? t.setData({
                    dispatch_types: s.data.dispatch_types,
                    showChoose: !0
                }) : t.submitGoods();
            }
        });
    },
    submitGoods: function() {
        if (this.data.showChoose && !this.data.radioChoose) return this.tips("请先选择配送方式");
        if (this.data.showChoose) {
            this.data.cart_ids = [];
            for (var t = 0; t < this.data.dispatch_types[this.data.radioChoose].member_carts.length; t++) this.data.cart_ids.push(this.data.dispatch_types[this.data.radioChoose].member_carts[t].id);
        }
        this.verifyFrom();
    },
    verifyFrom: function() {
        var t = this, a = [];
        a.push(t.data.good_ids);
        var e = s.getNetAddresss("from.div-from.isDisplay");
        e += "&goods_ids=" + JSON.stringify(a), s._getNetWork({
            showToastIn: !1,
            url: e,
            success: function(a) {
                var s = a.data;
                if (1 == s.result) {
                    var e = s.data;
                    console.log("xxxx", e), s.data.status && !s.data.member_status ? wx.showModal({
                        title: "提示",
                        content: "购买此商品,请补充您的个人信息",
                        success: function(t) {
                            t.confirm ? wx.navigateTo({
                                url: "/packageB/member/address/myinfo/myinfo?tag=delta"
                            }) : t.cancel && console.log("用户点击取消");
                        }
                    }) : wx.navigateTo({
                        url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-1&cart_ids=" + JSON.stringify(t.data.cart_ids)
                    });
                } else wx.showToast({
                    icon: "none",
                    title: s.msg,
                    duration: 1500
                });
            },
            fail: function(t) {}
        });
    },
    onReachBottom: function() {
        this.data.isLoadMore ? this._getMoreData() : console.log("没有更多数据");
    },
    tips: function(t) {
        return wx.showToast({
            title: t,
            icon: "none"
        }), !1;
    },
    onShareAppMessage: function() {
        var t = wx.getStorageSync("yz_uid"), a = "";
        return t && (a = t), {
            title: this.data.activity_data.activity_name,
            path: "/packageE/community_solitaire/community_solitaire?activity_id=" + this.data.activity_id + "&mid=" + a
        };
    }
});